<?php
	include_once("./global.php");
	session_start();
	if($_SESSION[id]){
		if($_POST['insert_category']){
			$cname = $_POST['category_title'];
			$sql_select_kid = "select * from keywordinfo where K_Name = '$cname'";
			$query_select_kid = $mysqli->query($sql_select_kid);
			$result_select_kid = $query_select_kid->fetch_assoc();
			if($result_select_kid){
				$kname = $result_select_kid['K_Name'];
				$sql_select_cid = "select * from appcategoryinfo where AC_Categoryname = '$kname'";
				$query_select_cid = $mysqli->query($sql_select_cid);
				$result_select_cid = $query_select_cid->fetch_assoc();
				if($result_select_cid){
					echo "<script>alert('该关键词已经存在')</script>";
				}else{
					$sql_insert_cname = "insert into appcategoryinfo (AC_Categoryid,AC_Categoryname) values (NULL, '$cname')";
					$query_insert_cname = $mysqli->query($sql_insert_cname);
					if($query_insert_cname){
						echo "<script>alert('appcategory添加成功')</script>";
						echo "<meta http-equiv=\"refresh\" content=\"2; url='admin_appcate_mangerment.php?page=1'\" />";
					}else{
						echo "<script>alert('appcategory添加失败')</script>";
						echo $mysqli->error;
					}	
				}
			}else{
				$sql_insert_kid = "INSERT INTO `keywordinfo`(`K_KID`, `K_ID`, `K_Name`, `K_search`, `K_tags`) VALUES (NULL,NULL,'$cname',NULL,'0')";
				$query_insert_kid = $mysqli->query($sql_insert_kid);
				if($query_insert_kid){
					$insert_cname = $cname;
					$sql_insert_cid = "insert into appcategoryinfo (AC_Categoryid,AC_Categoryname) values(NULL,'$cname')";
					$query_insert_cid = $mysqli->query($sql_insert_cid);
					if($query_insert_cid){
						echo "<script>alert('appcategoryinfo添加成功')</script>";
						echo "<meta http-equiv=\"refresh\" content=\"2; url='admin_appcate_mangerment.php?page=1'\" />";
					}else{
						echo "<script>alert('appcategoryinfo添加失败')</script>";
						echo $mysqli->error;
					}
				}else{
					echo "<script>alert('keywordinfo添加失败')</script>";
					echo $mysqli->error;
				}
			}
		}
		
		if($_POST['delete_appcategory']){
			$delete_arr = $_POST['category_id'];
			print_r($delete_arr);
			foreach($delete_arr as $key => $value){
				$value = $delete_arr[$key];
				$sql_delete = "delete from appcategoryinfo where AC_Categoryid = '$value'";
				$query_delete = $mysqli->query($sql_delete);
				if($query_delete){
					echo "<script>alert('删除成功')</script>";
				}else{
					echo "<script>alert('删除失败')</script>";
					echo $mysqli->error;
				}
			}
		}
	
		$url = $_SERVER['REQUEST_URI'];
		$url_arr = parse_url($url);
		parse_str($url_arr['query'],$url_arr);
		
		$sql_count = "select * from appcategoryinfo";
		$query_count = $mysqli->query($sql_count);
		$total = $query_count->num_rows;
		$num = '25';
		
		$page = new Page($total,$num);
		

	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台首页</title>
<style>
	body{ background:#e7e7e7; margin:0; padding:0;}
	#container{ width:980px; height:auto; position:relative; margin:0 auto;}
	#container #header{ width:980px; height:90px; position:relative; background:#000; line-height:90px;color:#fff; font-size:24px; text-align:center;}
	.left{width:250px; height:auto; position:relative; float:left;}
	.left table{width:248px; height:auto; position:relative; float:left; background:#fff;}
	.left table td{ height:20px; text-align:center;}
	.left table .selected{ background:#73abe3;}
	#content{ width:730px; height:100%; position:relative; float:left; margin:2px 0px;}
	#content .title{ width:720px; height:25px; position:relative; float:left; line-height:25px; font-size:18px; color:#fff; background:#000;}
	form{margin:20px auto; width:720px; height:auto; position:relative; float:left;}
	form .inpuarea{ width:360px; height:150px;}
</style>
</head>
<body>
	<div id="container">
		<div id="header">
			818苹果站后台管理
		</div>
		<div class="left">
			<table>
				<tr><td style="background:#000000; color:#fff;">站点管理</td></tr>
				<tr><td><a href="admin_site_mangerment.php">全局管理</a></td></tr>
				<tr><td>静态页面管理</td></tr>
				<tr><td><a href="admin_link_mangerment.php?page=1">友情链接管理</a></td></tr>
				<tr><td><a href="admin_member_mangerment.php?page=1">会员管理</a></td></tr>
				<tr><td><a href="admin_tags_mangerment.php?page=1">标签管理</a></td></tr>
				<tr><td><a href="admin_search_mangerment.php">搜索管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">文章管理</td></tr>
				<tr><td><a href="admin_category_mangerment.php">文章分类管理</a></td></tr>
				<tr><td><a href="admin_news_add.php">发布文章</a></td></tr>
				<tr><td><a href="admin_news_mangerment.php?page=1">文章管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">应用管理</td></tr>
				<tr><td class="selected"><a href="admin_appcate_mangerment.php?page=1">应用分类管理</a></td></tr>
				<tr><td><a href="admin_app_add.php">添加应用</a></td></tr>
				<tr><td><a href="admin_app_mangerment.php?page=1">应用管理</a></td></tr>
				<tr><td><a href="admin_app_collection.php">应用采集</a></td></tr>
				<tr><td><a href="admin_app_collection_single.php">单应用采集</a></td></tr>
			</table>
		</div>
	<div id="content">
		<div class="title">APP分类管理</div>
		<form action="" method="post">
		<table style="border-bottom:1px solid #000; width:728px;">
			<tr>
				<td><label>添加新分类</label></td><td><input type="text" name="category_title" /></td>
			</tr>
			<tr>
				<td><input type="submit" name="insert_category" value="提交" /></td>
			</tr>
		</table>
		</form>
		<form action="" method="post" name="form1">
		<table>
			<tr>标签列表</tr>
			<tr>
				<td>APP分类编号</td>
				<td>APP分类名称</td>
			</tr>
			<?php
				$sql_appcateogry_select = "select * from appcategoryinfo order by AC_Categoryid Asc {$page->limit_start()}";
				$query_appcategory_select = $mysqli->query($sql_appcateogry_select);
				while($result_appcategory_select = $query_appcategory_select->fetch_assoc()){		
			?>
			<tr>
            	<td><input type="checkbox" name="category_id[]"  value=<?php echo $result_appcategory_select['AC_Categoryid']?> />
				<?php
                	echo $result_appcategory_select['AC_Categoryid'];
				?>
                </td>
				
					<?php
/*						$categoryid = $result_appcategory_select['AC_Categoryname'];
						$sql_kname = "select K_Name from keywordinfo where K_ID = '$categoryid'";
						$query_kname = $mysqli->query($sql_kname);
						$result_kname = $query_kname->fetch_assoc();*/
					?>
						<td><?php echo $result_appcategory_select['AC_Categoryname'];?></td>		
				<td></td>
			</tr>
			<?php
			}
			?>
			<tr>
				<td>
                	<input type="checkbox" id="quan" name="quan" onclick="select_all()" />全选
					<input type="submit" name="delete_appcategory" value="删除" onclick="del()" /> 
				</td>
			</tr>
			<tr>
				<?php echo $page->pageFirst();?>
				<?php echo $page->pagePre();?>
				<?php echo $page->pageList();?>
				<?php echo $page->pageNext();?>
				<?php echo $page->pageLast();?>
				<?php echo $page->pageTotal();?>
			</tr>
		</table>
		</form>
	</div>
</body>
<script language="javascript">
	function select_all(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(document.getElementById("quan").checked != false){
					checked.checked = true;
				}else{
					checked.checked = false;
				}
			}
		}
	}
	function del(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(checked.checked == true){
					alert(checked.name+checked.value);
				}
			}
		}
	}
	$(document).ready(function(){
		$("table").find("td").click(function(){
			src = $(this).attr("data-option-value");
			if(!$(this).hasClass("selected")){
				$("table").find("td").removeClass("selected");
				$(this).addClass("selected");
				$("iframe").attr("src",src);
			}			
		});
	});
</script>
</html>
<?php
	}else{
		echo "<script>alert('登陆先')</script>";
		echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_login.php' />";
	}
?>